package extend_widget

import (
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget"
	"clientDemo/ui/fyne_ui/widget/interface"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/driver/desktop"
	"image/color"
	"strconv"
	"strings"
)

var _ widgetcommon.IWidgetItem = (*Rectangle)(nil)

func init() {
	widget.RegWidget("WidgetRectangle", "矩形", NewRectangle)
}

// Rectangle 矩形
type Rectangle struct {
	*WidgetContainer
	Rectangle *canvas.Rectangle
	*Action
	rootNode widgetcommon.IWidgetItem //父节点

	// 矩形属性
	Width       float32
	Height      float32
	BorderWidth float32
	FillColor   color.RGBA
	BorderColor color.RGBA
}

func NewRectangle(name string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	if skinInfo == nil {
		return nil
	}

	rect := canvas.NewRectangle(color.RGBA{255, 255, 255, 255})
	_rectangle := &Rectangle{
		Rectangle: rect,
		Action:    &Action{},
		WidgetContainer: &WidgetContainer{
			Container: &Container{
				name: name,
			},
			Border: canvas.NewRectangle(color.RGBA{0, 0, 0, 0}), // 初始化Border
		},
		rootNode: rootNode,

		// 默认值
		Width:       100,
		Height:      60,
		BorderWidth: 2,
		FillColor:   color.RGBA{255, 255, 255, 255},
		BorderColor: color.RGBA{0, 0, 0, 255},
	}

	// 从皮肤信息中读取属性
	_rectangle.loadFromSkin(skinInfo)

	// 应用颜色和边框设置到canvas.Rectangle
	_rectangle.Rectangle.FillColor = _rectangle.FillColor
	_rectangle.Rectangle.StrokeColor = _rectangle.BorderColor
	_rectangle.Rectangle.StrokeWidth = _rectangle.BorderWidth
	// 确保边框可见
	if _rectangle.BorderWidth > 0 {
		_rectangle.Rectangle.StrokeColor = _rectangle.BorderColor
	}

	// 设置矩形尺寸
	_rectangle.Rectangle.Resize(fyne.NewSize(_rectangle.Width, _rectangle.Height))

	// 初始化IUserInfo
	_rectangle.IUserInfo = common.NewWidgetUserInfo(name, skinInfo.GetSkinName(), _rectangle)
	_rectangle.WidgetContainer.Container.IUserInfo = _rectangle.IUserInfo
	_rectangle.WidgetContainer.ac = _rectangle.Action
	_rectangle.WidgetContainer.SetObjects([]fyne.CanvasObject{_rectangle.Rectangle})
	_rectangle.SetBase(_rectangle, rootNode)
	_rectangle.ExtendBaseWidget(_rectangle)

	return _rectangle
}

func (r *Rectangle) loadFromSkin(skinInfo widgetcommon.IWidgetSkin) {
	// 从属性列表中查找属性
	attributes := skinInfo.GetAttribute()
	for _, attr := range attributes {
		switch attr.Key {
		case "Width":
			if width, err := strconv.ParseFloat(attr.Value, 32); err == nil {
				r.Width = float32(width)
			}
		case "Height":
			if height, err := strconv.ParseFloat(attr.Value, 32); err == nil {
				r.Height = float32(height)
			}
		case "BorderWidth":
			if borderWidth, err := strconv.ParseFloat(attr.Value, 32); err == nil {
				r.BorderWidth = float32(borderWidth)
			}
		case "FillColor":
			r.FillColor = parseColor(attr.Value)
		case "BorderColor":
			r.BorderColor = parseColor(attr.Value)
		}
	}
}

func parseColor(colorStr string) color.RGBA {
	parts := strings.Split(colorStr, " ")
	if len(parts) >= 3 {
		// 解析RGB值
		r, _ := strconv.Atoi(parts[0])
		g, _ := strconv.Atoi(parts[1])
		b, _ := strconv.Atoi(parts[2])
		a := 255 // 默认不透明

		// 如果有4个参数，第四个参数是alpha值
		if len(parts) == 4 {
			a, _ = strconv.Atoi(parts[3])
		}

		return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
	}
	return color.RGBA{255, 255, 255, 255}
}

func (r *Rectangle) GetName() string {
	return r.name
}

func (r *Rectangle) SetName(name string) {
	r.name = name
}

func (r *Rectangle) GetSize() fyne.Size {
	return fyne.NewSize(r.Width, r.Height)
}

func (r *Rectangle) Resize(size fyne.Size) {
	r.Width = size.Width
	r.Height = size.Height
	r.Rectangle.Resize(size)
}

func (r *Rectangle) GetPosition() fyne.Position {
	return r.Rectangle.Position()
}

// Move 千万不要重载
//func (r *Rectangle) Move(pos fyne.Position) {
//	r.WidgetContainer.Move(pos)
//}
// Move2 千万不要重载
//func (r *Rectangle) Move2(pos fyne.Position) {
//	r.WidgetContainer.Move(pos)
//}

func (r *Rectangle) GetMinSize() fyne.Size {
	return fyne.NewSize(r.Width, r.Height)
}

func (r *Rectangle) Show() {
	r.Rectangle.Show()
}

func (r *Rectangle) Hide() {
	r.Rectangle.Hide()
}

func (r *Rectangle) Visible() bool {
	return r.Rectangle.Visible()
}

func (r *Rectangle) Refresh() {
	r.Rectangle.Refresh()
}

func (r *Rectangle) GetParent() widgetcommon.IWidgetItem {
	return r.rootNode
}

func (r *Rectangle) SetParent(parent widgetcommon.IWidgetItem) {
	r.rootNode = parent
}

func (r *Rectangle) AddWidgetChild(child widgetcommon.IWidgetItem, pos fyne.Position) {
	// 矩形控件通常不包含子控件
}

func (r *Rectangle) RemoveWidgetChild(child widgetcommon.IWidgetItem) {
	// 矩形控件通常不包含子控件
}

func (r *Rectangle) ForEach(fn func(widgetcommon.IWidgetItem) bool) {
	// 矩形控件通常不包含子控件
}

func (r *Rectangle) GetCanvasObject() fyne.CanvasObject {
	return r.Rectangle
}

func (r *Rectangle) SetProperty(key, value string, isProperty bool) {
	r.WidgetContainer.SetProperty(key, value, isProperty)
	// 先处理矩形特定属性
	switch key {
	case "Width":
		if width, err := strconv.ParseFloat(value, 32); err == nil {
			r.Width = float32(width)
			r.Rectangle.Resize(fyne.NewSize(r.Width, r.Height))
			r.Refresh()
		}
	case "Height":
		if height, err := strconv.ParseFloat(value, 32); err == nil {
			r.Height = float32(height)
			r.Rectangle.Resize(fyne.NewSize(r.Width, r.Height))
			r.Refresh()
		}
	case "BorderWidth":
		if borderWidth, err := strconv.ParseFloat(value, 32); err == nil {
			r.BorderWidth = float32(borderWidth)
			r.Rectangle.StrokeWidth = r.BorderWidth
			// 确保边框可见
			if r.BorderWidth > 0 {
				r.Rectangle.StrokeColor = r.BorderColor
			}
			r.Refresh()
		}
	case "FillColor":
		r.FillColor = parseColor(value)
		r.Rectangle.FillColor = r.FillColor
		r.Refresh()
	case "BorderColor":
		r.BorderColor = parseColor(value)
		r.Rectangle.StrokeColor = r.BorderColor
		r.Refresh()
	default:
		// 其他属性交给父类处理
		r.WidgetContainer.SetProperty(key, value, isProperty)
	}
}
func (r *Rectangle) ForEachProperty(cb func(k, v string)) {
	r.IUserInfo.ForEachProperty(func(key, value string) {
		cb(key, value)
	})
}
func (r *Rectangle) GetSkinTempName() string {
	return r.IUserInfo.GetSkinTempName()
}

func (r *Rectangle) SetSkinTempName(name string) {
	// IUserInfo接口没有SetSkinTempName方法，这里可以添加自定义逻辑
}

func (r *Rectangle) MouseIn(event *fyne.PointEvent) {
	// 实现鼠标进入事件
}

func (r *Rectangle) MouseOut() {
	// 实现鼠标离开事件
}

func (r *Rectangle) Tapped(event *fyne.PointEvent) {
	// 实现点击事件
}

func (r *Rectangle) TappedSecondary(event *fyne.PointEvent) {
	// 实现右键点击事件
}

func (r *Rectangle) DoubleTapped(event *fyne.PointEvent) {
	// 实现双击事件
}

func (r *Rectangle) FocusGained() {
	// 实现获得焦点事件
}

func (r *Rectangle) FocusLost() {
	// 实现失去焦点事件
}

func (r *Rectangle) TypedRune(rune rune) {
	// 实现字符输入事件
}

func (r *Rectangle) TypedKey(key *fyne.KeyEvent) {
	// 实现按键事件
}

func (r *Rectangle) DragEnd() {
	// 实现拖拽结束事件
}

func (r *Rectangle) Dragged(event *fyne.DragEvent) {
	// 实现拖拽事件
}

func (r *Rectangle) CreateRenderer() fyne.WidgetRenderer {
	return r.WidgetContainer.CreateRenderer()
}

func (r *Rectangle) MinSize() fyne.Size {
	return fyne.NewSize(r.Width, r.Height)
}

func (r *Rectangle) Position() fyne.Position {
	return r.WidgetContainer.Position()
}

func (r *Rectangle) Size() fyne.Size {
	return fyne.NewSize(r.Width, r.Height)
}

func (r *Rectangle) Hidden() bool {
	return !r.Rectangle.Visible()
}

func (r *Rectangle) SetVisible(visible bool) {
	if visible {
		r.Rectangle.Show()
	} else {
		r.Rectangle.Hide()
	}
}

func (r *Rectangle) Cursor() desktop.Cursor {
	return desktop.DefaultCursor
}

func (r *Rectangle) SetCursor(cursor desktop.Cursor) {
	// canvas.Rectangle不支持设置光标
}
